package com.suncaper.han_yi_system.controller;

import com.suncaper.han_yi_system.pojo.entity.SizeRecommendation;
import com.suncaper.han_yi_system.result.Result;
import com.suncaper.han_yi_system.service.SizeRecommendationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Objects;

@RestController
@RequestMapping("/sizeRecommendation")
@CrossOrigin(origins = "*")
@Tag(name = "尺码管理")
@Slf4j
public class SizeRecommendationController {

    @Autowired
    SizeRecommendationService sizeRecommendationService;

    @Operation(summary = "新增推荐尺码")
    @PostMapping("/save")
    public Result save(@RequestBody SizeRecommendation sizeRecommendation) {
        sizeRecommendation.setCreateTime(LocalDateTime.now());
        sizeRecommendationService.save(sizeRecommendation);
        return Result.ok();
    }

    @Operation(summary = "更新推荐尺码")
    @PutMapping("/update")
    public Result update(@RequestBody SizeRecommendation sizeRecommendation) {
        sizeRecommendation.setUpdateTime(LocalDateTime.now());
        sizeRecommendationService.updateById(sizeRecommendation);
        return Result.ok();
    }

    @Operation(summary = "删除推荐尺码")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        if(Objects.isNull(id)){
            return Result.error().message("id不能为空");
        }
        sizeRecommendationService.removeById(id);
        return Result.ok();
    }

    @Operation(summary = "根据id获取推荐尺码")
    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable Long id) {
        if(Objects.isNull(id)){
            return Result.error().message("id不能为空");
        }
        return Result.ok().data("sizeRecommendation", sizeRecommendationService.getById(id));
    }

    @Operation(summary = "获取推荐尺码列表")
    @RequestMapping("/list")
    public Result getList(){
        return Result.ok().data("sizeRecommendationList", sizeRecommendationService.list());
    }

}
